﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class bingkejiezhang : Form
    {
        public bingkejiezhang()
        {
            InitializeComponent();
        }
        public int czno;
        DataSet ds = new DataSet();
        string constr = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
        private void bingkejiezhang_Load(object sender, EventArgs e)
        {
            label27.Text =Convert.ToString( czno);
            chadingdan();
            label17.Text = Convert.ToString(a);
            label19.Text = "0";
            huoqudingdan();
            if (label15.Text.ToString().Equals("无"))
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["yingshoujine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["yingshoujine"].Value) * 1);
                    dataGridView1.Rows[i].Cells["dazhebili"].Value = Convert.ToString(1.0);
                    dataGridView1.Rows[i].Cells["zhehoudanjia"].Value = dataGridView1.Rows[i].Cells["danjia"].Value;
                    dataGridView1.Rows[i].Cells["dazhejine"].Value = Convert.ToString(0.0);
                }
            }
            
        }
        public void huoqudingdan()
        {
            string sql = @"SELECT  [Ordering management].dcCzid, commodity.spName, commodity.spJia, [Ordering management].dcShul, 
                   [Ordering management].dcJine
FROM      commodity INNER JOIN
                   [Ordering management] ON commodity.spID = [Ordering management].dcName
                   where dcCzid="+czno+" ";
            SqlConnection sqlcon = new SqlConnection(constr);
            SqlDataAdapter adapter = new SqlDataAdapter(sql,sqlcon);
            adapter.Fill(ds,"diancan");
            dataGridView1.DataSource=ds.Tables["diancan"];

        }
        public void vipcha()
        {
            ds = new DataSet("vip");
            if (ds.Tables["vip"]!=null)
            {
                ds.Tables["vip"].Clear();
            }
            string sql = @"SELECT  VIPuser.vipName, VIPtype.vipLeiName
FROM      VIPtype INNER JOIN
                   VIPuser ON VIPtype.vipLeiHao = VIPuser.vipLei";
            SqlConnection sqlcon = new SqlConnection(constr);
            SqlDataAdapter adapter = new SqlDataAdapter(sql,sqlcon);
            adapter.Fill(ds,"vip");
            dataGridView2.DataSource=ds.Tables["vip"];
            
        }
        double a = 0;
        public void chadingdan()
        {
            string sql = @"SELECT  dcJine
FROM      [Ordering management] where dcCzid="+czno+"";
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql,sqlcon);
            SqlDataReader reader = sqlcom.ExecuteReader();
             a = 0;
            while (reader.Read())
            {
                a = a + Convert.ToDouble(reader["dcJine"]);
            }
            sqlcon.Close();
            label4.Text =Convert.ToString( czno);
            label6.Text = Convert.ToString(a);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            vipcha();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
           
            string sql = @"SELECT  VIPuser.*, VIPtype.*
FROM      VIPtype INNER JOIN
                   VIPuser ON VIPtype.vipLeiHao = VIPuser.vipLei where vipName='"+Convert.ToString( dataGridView2.SelectedRows[0].Cells["xingming"].Value)+"'";
            SqlConnection sqlcon=new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom=new SqlCommand(sql,sqlcon);
            SqlDataReader reader=sqlcom.ExecuteReader();
            if (reader.Read())
	{
        textBox1.Text =Convert.ToString( reader["vipID"]);
        label8.Text = Convert.ToString(reader["vipName"]);
        label11.Text = Convert.ToString(reader["vipLeiName"]);
        label13.Text = Convert.ToString(reader["vipMorey"]);
        label15.Text = Convert.ToString(reader["vipzhekou"]);
        if (label15.Text.ToString().Equals("1"))
        {
            label15.Text = "无";
        }
	}
            
            panel1.Visible = false;
            
        }

        private void label15_TextChanged(object sender, EventArgs e)
        {
            double a = 0,b=0;
            if (label15.Text.ToString().Equals("1"))
            {
                label15.Text = "无";
            }
            if (label15.Text.ToString().Equals("无"))
            {
                label19.Text = "无";
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["yingshoujine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["yingshoujine"].Value) * 1);
                }
                a = Convert.ToDouble(label17.Text);
                label30.Text = Convert.ToString(a);
                label19.Text = Convert.ToString((Convert.ToDouble(label17.Text) - a));
                label21.Text = Convert.ToString(a);
                if (textBox2.Text == string.Empty)
                {
                    label24.Text = Convert.ToString(0 - Convert.ToDouble(label21.Text));
                }
                a = 0;
            }
            else
            {
                label19.Text = Convert.ToString(a / Convert.ToDouble(label15.Text));
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["yingshoujine"].Value = Convert.ToString((Convert.ToDouble(dataGridView1.Rows[i].Cells["danjia"].Value) * Convert.ToInt32( dataGridView1.Rows[i].Cells["xiaofeishuliang"].Value)) * Convert.ToDouble(label15.Text) / 10);
                    dataGridView1.Rows[i].Cells["dazhebili"].Value = Convert.ToString(Convert.ToDouble(label15.Text)/10);
                    dataGridView1.Rows[i].Cells["zhehoudanjia"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["danjia"].Value) * Convert.ToDouble(label15.Text) / 10);
                    dataGridView1.Rows[i].Cells["dazhejine"].Value = Convert.ToString(Convert.ToDouble(dataGridView1.Rows[i].Cells["danjia"].Value)-Convert.ToDouble(dataGridView1.Rows[i].Cells["danjia"].Value) * Convert.ToDouble(label15.Text) / 10);
                    a = a + Convert.ToDouble(dataGridView1.Rows[i].Cells["yingshoujine"].Value);
                }
                label30.Text = Convert.ToString(a);
                label19.Text = Convert.ToString((Convert.ToDouble(label17.Text)-a));
                label21.Text = Convert.ToString(a);
                if (textBox2.Text==string.Empty)
                {
                    label24.Text = Convert.ToString(0-Convert.ToDouble(label21.Text));
                }
                a = 0;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == string.Empty)
            {
                label24.Text = Convert.ToString(0 - Convert.ToDouble(label21.Text));
            }
            else if (textBox2.Text.Trim().Substring(0, 1).Equals("-") && textBox2.Text.Trim().Length > 1)
            {
                label24.Text = Convert.ToString(Convert.ToDouble(textBox2.Text.Trim()) + (-Convert.ToDouble(label21.Text)));
            }
            else if(!textBox2.Text.Trim().Substring(0,1).Equals("-"))
            {
                label24.Text = Convert.ToString(Convert.ToDouble(textBox2.Text) - Convert.ToDouble(label21.Text));
            }
          }
        youhui you = new youhui();
        private void button2_Click(object sender, EventArgs e)
        {
            you.shuoming = null;
            you.youjine = null;
            you.jine = Convert.ToString(label21.Text);
            you.ShowDialog();
            
        }

        public string youhui1="0";
        public string yinfu = "0";
        private void bingkejiezhang_Activated(object sender, EventArgs e)
        {
            youhui1 = you.jine;

           
            if (Convert.ToDouble( youhui1)>0)
            {
                label21.Text = youhui1;
                textBox3.Text = you.shuoming;
                label19.Text = you.youjine;
                label24.Text = Convert.ToString(0 - Convert.ToDouble(label21.Text));
            }
            yinfu = lianhe.jia;
            if (Convert.ToDouble(yinfu)==0&&b==1)
            {
                string sql = "delete [Ordering management] where dcCzid='"+label4.Text.Trim()+"'";
                SqlConnection sqlcon = new SqlConnection(constr);
                sqlcon.Open();
                SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
                this.Close();
            }
            
            
        }
        string xuanzhi;
        string xuanzhi2;
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (xuanzhi==null&&xuanzhi2==null)
            {
                 xuanzhi = label21.Text.Trim();
                 xuanzhi2 = textBox2.Text.Trim(); 
            }
            
            if (checkBox1.Checked==true)
            {
                label21.Enabled = false;
                textBox2.Enabled = false;
                label21.Text = "0";
                textBox2.Text = "0";
                checkBox2.Enabled = false;
                checkBox3.Enabled = false;
            }
            else
            {
                label21.Enabled = true;
                textBox2.Enabled = true;
                label21.Text = xuanzhi;
                textBox2.Text = xuanzhi2;
                checkBox2.Enabled = true;
                checkBox3.Enabled = true;
            }
            
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (xuanzhi == null && xuanzhi2 == null)
            {
                xuanzhi = label21.Text.Trim();
                xuanzhi2 = textBox2.Text.Trim();
            }
            if (checkBox2.Checked == true)
            {
                label21.Enabled = false;
                textBox2.Enabled = false;
                label21.Text = "0";
                textBox2.Text = "0";
                checkBox1.Enabled = false;
                checkBox3.Enabled = false;
            }
            else
            {
                label21.Enabled = true;
                textBox2.Enabled = true;
                label21.Text = xuanzhi;
                textBox2.Text = xuanzhi2;
                checkBox1.Enabled = true;
                checkBox3.Enabled = true;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (xuanzhi == null && xuanzhi2 == null)
            {
                xuanzhi = label21.Text.Trim();
                xuanzhi2 = textBox2.Text.Trim();
            }
            if (checkBox3.Checked == true)
            {
                label21.Enabled = false;
                textBox2.Enabled = false;
                label21.Text = "0";
                textBox2.Text = "0";
                checkBox2.Enabled = false;
                checkBox1.Enabled = false;
            }
            else
            {
                label21.Enabled = true;
                textBox2.Enabled = true;
                label21.Text = xuanzhi;
                textBox2.Text = xuanzhi2;
                checkBox2.Enabled = true;
                checkBox1.Enabled = true;
            }
        }
        lianhejiezhang lianhe = new lianhejiezhang();
        int b = 0;
        private void button4_Click(object sender, EventArgs e)
        {
            b = 1;
            lianhe.jia = label21.Text.Trim();
            lianhe.cz = label4.Text;
            lianhe.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "delete [Ordering management] where dcCzid='" + label4.Text.Trim() + "'";
            SqlConnection sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            sqlcom.ExecuteNonQuery();
            
            string sql1 = "update [Table]  set cz='2'   where czID='" + czno + "'";           
            SqlCommand sqlcom1 = new SqlCommand(sql1, sqlcon);
            sqlcom1.ExecuteNonQuery();
            sqlcon.Close();
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        } 

    }
}
